#include "ScheduleManager.h"

static ScheduleManager* mInstance = NULL;

void
ScheduleManager::initialize( QWidget* parent )
{
	if( mInstance == NULL )
	{
		mInstance = new ScheduleManager( parent );
	}
}

ScheduleManager*
ScheduleManager::instance()
{
	return mInstance;
}

ScheduleManager::ScheduleManager( QWidget* parent )
	: QSplitter( Qt::Vertical, parent )
{
	mTableView = new QTableView( this );
	addWidget( mTableView );
	mTableView->setModel( Database::instance()->scheduleModel() ); 
	connect( mTableView, SIGNAL( clicked( const QModelIndex& ) ), this, SLOT( selected( const QModelIndex& ) ) );

	QGroupBox* editGroup = new QGroupBox( "Schedule Item", this );
	QVBoxLayout* layout = new QVBoxLayout( editGroup );
	addWidget( editGroup );

	mNameInput = new QLineEdit( editGroup );
	mProgramInput = new QLineEdit( editGroup );
	mFacultyInput = new QLineEdit( editGroup );
	mRoomInput = new QLineEdit( editGroup );
	mCourseInput = new QLineEdit( editGroup );

	mProgramSelector = new QComboBox( editGroup );
	mFacultySelector = new QComboBox( editGroup );
	mRoomSelector = new QComboBox( editGroup );
	mCourseSelector = new QComboBox( editGroup );

	connect( mProgramSelector, SIGNAL( activated( const QString& ) ), mProgramInput, SLOT( setText( const QString& ) ) );
	connect( mFacultySelector, SIGNAL( activated( const QString& ) ), mFacultyInput, SLOT( setText( const QString& ) ) );
	connect( mRoomSelector, SIGNAL( activated( const QString& ) ), mRoomInput, SLOT( setText( const QString& ) ) );
	connect( mCourseSelector, SIGNAL( activated( const QString& ) ), mCourseInput, SLOT( setText( const QString& ) ) );

    mProgramSelector->setModel( Database::instance()->programModel() );
    mProgramSelector->setModelColumn( Database::instance()->programModel()->fieldIndex( "id" ) );
    mFacultySelector->setModel( Database::instance()->facultyModel() );
    mFacultySelector->setModelColumn( Database::instance()->facultyModel()->fieldIndex( "id" ) );
    mRoomSelector->setModel( Database::instance()->roomModel() );
    mRoomSelector->setModelColumn( Database::instance()->roomModel()->fieldIndex( "id" ) );
    mCourseSelector->setModel( Database::instance()->courseModel() );
    mCourseSelector->setModelColumn( Database::instance()->courseModel()->fieldIndex( "id" ) );

	mMapper = new QDataWidgetMapper( this );
	mMapper->setModel( Database::instance()->scheduleModel() );
	mMapper->setItemDelegate( new QSqlRelationalDelegate( this ) );
	mMapper->setSubmitPolicy( QDataWidgetMapper::AutoSubmit );
	mMapper->addMapping( mNameInput, 0 );
	mMapper->addMapping( mProgramInput, 1 );
	mMapper->addMapping( mFacultyInput, Database::instance()->scheduleModel()->fieldIndex( "facultyId" ) );
	mMapper->addMapping( mRoomInput, Database::instance()->scheduleModel()->fieldIndex( "roomId" ) );
	mMapper->addMapping( mCourseInput, Database::instance()->scheduleModel()->fieldIndex( "courseId" ) );
	mMapper->toFirst();

	QHBoxLayout* nameLayout = new QHBoxLayout();
	QHBoxLayout* programLayout = new QHBoxLayout();
	QHBoxLayout* facultyLayout = new QHBoxLayout();
	QHBoxLayout* roomLayout = new QHBoxLayout();
	QHBoxLayout* courseLayout = new QHBoxLayout();

	layout->addItem( nameLayout );
	layout->addItem( programLayout );
	layout->addItem( facultyLayout );
	layout->addItem( roomLayout );
	layout->addItem( courseLayout );

	nameLayout->addWidget( new QLabel( "Name:", editGroup ) );
	programLayout->addWidget( new QLabel( "Program:", editGroup ) );
	facultyLayout->addWidget( new QLabel( "Faculty:", editGroup ) );
	roomLayout->addWidget( new QLabel( "Room:", editGroup ) );
	courseLayout->addWidget( new QLabel( "Course:", editGroup ) );

	nameLayout->addWidget( mNameInput );
	programLayout->addWidget( mProgramInput );
	facultyLayout->addWidget( mFacultyInput );
	roomLayout->addWidget( mRoomInput );
	courseLayout->addWidget( mCourseInput );

	programLayout->addWidget( mProgramSelector );
	facultyLayout->addWidget( mFacultySelector );
	roomLayout->addWidget( mRoomSelector );
	courseLayout->addWidget( mCourseSelector );
}

ScheduleManager::~ScheduleManager()
{
}

QList<ScheduleItem*> 
ScheduleManager::getProgramSchedule( int id )
{
	QAbstractItemModel* model = Database::instance()->scheduleModel();

	QList<ScheduleItem*> s;

	int i = 0;

	while( model->hasIndex( i, 0 ) )
	{
		int programId = model->data( model->index( i, 1 ) ).toInt();

		if( programId == id )
		{
			s << new ScheduleItem( model->data( model->index( i, 0 ) ).toInt(),
								   programId,
								   model->data( model->index( i, 2 ) ).toInt(),
								   model->data( model->index( i, 0 ) ).toInt(),
								   model->data( model->index( i, 0 ) ).toInt() );
		}

		i++;
	}

	return s;
}

QList<ScheduleItem*>
ScheduleManager::getCourseSchedule( int id )
{
	QList<ScheduleItem*> s;
	return s;
}

QList<ScheduleItem*>
ScheduleManager::getFacultySchedule( int id )
{
	QList<ScheduleItem*> s;
	return s;
}

QList<ScheduleItem*>
ScheduleManager::getRoomSchedule( int id )
{
	QList<ScheduleItem*> s;
	return s;
}

void 
ScheduleManager::addItem( ScheduleItem* item )
{
	Database::instance()->query( tr( "insert into " ) + 
					                  SCHEDULE + 
									  tr( " values(" ) + 
									  QString::number( item->id() ) + 
									  tr( ", " ) +
									  QString::number( item->program() ) + 
									  tr( ", " ) +
									  QString::number( item->faculty() ) + 
									  tr( ", " ) +
									  QString::number( item->room() ) + 
									  tr( ", " ) +
									  QString::number( item->course() ) + 
									  tr( ")" ) );
}

void
ScheduleManager::selected( const QModelIndex& index )
{
	mMapper->setCurrentIndex( index.row() );
}